package misc

import java.util.{Calendar, Date}

/**
  * Created by shichen on 2017/6/26.
  * http://www.cnblogs.com/flyingzl/articles/3114412.html
  */

object DateHelper {
  val ago = "ago"
  val fromNow = "fromNow"
  implicit def convert(number: Int) = new DateHelper(number)
}

class DateHelper private(number: Int) {
  def days(when: String): Date = {
    val date = Calendar.getInstance()
    when match {
      case DateHelper.ago => date.add(Calendar.DAY_OF_MONTH, -number)
      case DateHelper.fromNow => date.add(Calendar.DAY_OF_MONTH, number)
      case _ => date
    }
    date.getTime
  }
}

object ScalaDsl extends App{
  import DateHelper._
  val twoDayAgo = 2 days ago
  val twoDayAfter = 2 days fromNow
  println("two days ago is " + twoDayAgo)
}
